//
//  PersonalViewController.m
//  ReactiveCocoa
//
//  Created by admin on 2022/3/22.
//

#import "PersonalViewController.h"
#import "YMWebViewController.h"
#import "AdviceViewController.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import <MJExtension/MJExtension.h>


@interface PersonalViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UIView *headerView;
@property (nonatomic,strong)UILabel *phoneLable;
@property (nonatomic,strong)UILabel *idLable;
@property (nonatomic,strong)UIImageView *headImageView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *footerView;
@property (nonatomic,strong)UIButton *logoutButton;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation PersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我";
    self.dataArray = [NSMutableArray array];
    NSArray *data = @[
      
        @{
            @"title" : @"意见反馈",
            @"icon" : @"Menu_Advice"
            
        },
        @{
            @"title" : @"关于我们",
            @"icon" : @"Menu_About"
            
        },
        @{
            @"title" : @"SDK版本(V1.0.0)",
            @"icon" : @""
            
        }
        
    ];
    
    [self.dataArray addObjectsFromArray:data];
    [self.view addSubview:self.tableView];
    [self initView];
    [self bindData];
   
}
- (void)bindData {
    
    NSDictionary *dic = (NSDictionary *)[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
    YMUserInfo *userInfo = [YMUserInfo mj_objectWithKeyValues:dic];
    self.phoneLable.text = userInfo.userName;
    self.idLable.text = [NSString stringWithFormat:@"用户ID:%@",userInfo.userId];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.userAvatar] placeholderImage:nil options:SDWebImageRetryFailed];
    
    
}
-(void)initView{
    
    [self.headerView addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.headerView.mas_centerY);
        make.left.mas_equalTo(self.headerView.mas_left).offset(15);
        make.size.mas_equalTo(CGSizeMake(55, 55));
    }];
    
    [self.headerView addSubview:self.phoneLable];
    [self.phoneLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.headImageView.mas_top).offset(3);
        make.left.mas_equalTo(self.headImageView.mas_right).offset(15);
   
    }];
    
    [self.headerView addSubview:self.idLable];
    [self.idLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self.headImageView.mas_bottom).offset(-3);
        make.left.mas_equalTo(self.phoneLable.mas_left);
       
    }];
    
    [self.footerView addSubview:self.logoutButton];
    [self.logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.footerView).offset(55);
        make.right.equalTo(self.footerView).offset(-55);
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self.footerView.mas_bottom);
    }];
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 2;
    }else{
        return 1;
    }

 
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    NSDictionary *model ;
    if (indexPath.section==1) {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        model =  [self.dataArray objectAtIndex:indexPath.section+1];

    }else{
        
        model =  [self.dataArray objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   
    }
    [cell.imageView setImage:[UIImage imageNamed:[model valueForKey:@"icon"]]];
    cell.textLabel.text = [model valueForKey:@"title"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {
      
        if (indexPath.row == 1) {
            
            YMWebViewController *webViewController = [[YMWebViewController alloc]init];
            webViewController.title = @"关于我们";
            webViewController.hidesBottomBarWhenPushed = YES;
            [webViewController loadURL:@"www.baidu.com"];
            [self.navigationController pushViewController:webViewController animated:YES];
            
        }else{
            
            AdviceViewController *vc = [[AdviceViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
   return 55;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
    return view;
}
-(void)logoutAction{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"退出登录" message:@"确定要退出登录吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
        
        [[YMIMClient sharedClient] logout];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:confirm];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

- (UIButton *)logoutButton {
    if (!_logoutButton) {
        _logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [_logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _logoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _logoutButton.layer.cornerRadius = 20;
        _logoutButton.layer.masksToBounds = YES;
        _logoutButton.backgroundColor = [UIColor colorWithRed:72/255.0 green:142/255.0 blue:255/255.0 alpha:1];
        [_logoutButton addTarget:self action:@selector(logoutAction) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
    }
    
    return _logoutButton;
}
-(UIView *)footerView{
    if (_footerView == nil) {
        
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        _footerView.backgroundColor = [UIColor whiteColor];
        
    }
    
    return _footerView;
}
-(UIView *)headerView{
    if (_headerView == nil) {
        
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        _headerView.backgroundColor = [UIColor whiteColor];
        
    }
    
    return _headerView;
}
-(UIImageView *)headImageView{
    
    if (_headImageView == nil) {
        
        _headImageView = [[UIImageView alloc]init];
        _headImageView.backgroundColor = [UIColor orangeColor];
        _headImageView.layer.cornerRadius = 27.5;
        _headImageView.clipsToBounds = YES;
    }
    
    return _headImageView;
}
-(UILabel *)idLable{
    
    if (_idLable == nil) {
    
        _idLable = [[UILabel alloc]init];
        _idLable.text = @"ID：6666";
        _idLable.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _idLable.font = [UIFont systemFontOfSize:16];
    }
    return _idLable;
}
-(UILabel *)phoneLable{
    
    if (_phoneLable == nil) {
    
        _phoneLable = [[UILabel alloc]init];
        _phoneLable.text = @"13357681909";
        _phoneLable.textColor =[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];;
        _phoneLable.font = [UIFont systemFontOfSize:16];
    }
    return _phoneLable;
}

- (UITableView *)tableView {
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.estimatedRowHeight           = 44;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.bounces = NO;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
        }
       
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
